package com.example.scenictourback.controller;

import com.example.scenictourback.common.Result;
import com.example.scenictourback.dto.ScenicSpotDTO;
import com.example.scenictourback.entity.ScenicSpot;
import com.example.scenictourback.service.ScenicSpotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class ScenicSpotController {
    
    @Autowired
    private ScenicSpotService scenicSpotService;
    
    /**
     * 获取景区下的所有景点 (原端点)
     */
    @GetMapping("/scenic-spot/list")
    public Result<?> listScenicSpotsByScenicAreaId(@RequestParam Integer scenicAreaId) {
        List<ScenicSpotDTO> scenicSpots = scenicSpotService.listScenicSpotsByScenicAreaId(scenicAreaId);
        return Result.success(scenicSpots);
    }
    
    /**
     * 获取景区下的所有景点 (新端点，匹配前端调用)
     */
    @GetMapping("/scenicspot/scenic/{scenicAreaId}")
    public Result<?> getScenicSpotsByScenicAreaId(@PathVariable Integer scenicAreaId) {
        List<ScenicSpotDTO> scenicSpots = scenicSpotService.listScenicSpotsByScenicAreaId(scenicAreaId);
        return Result.success(scenicSpots);
    }
    
    /**
     * 根据ID获取景点详情
     */
    @GetMapping("/scenic-spot/{id}")
    public Result<?> getScenicSpotById(@PathVariable Integer id) {
        ScenicSpotDTO scenicSpot = scenicSpotService.getScenicSpotById(id);
        return Result.success(scenicSpot);
    }
    
    /**
     * 根据名称搜索景点
     */
    @GetMapping("/scenic-spot/search")
    public Result<?> searchScenicSpotsByName(@RequestParam String name) {
        List<ScenicSpotDTO> scenicSpots = scenicSpotService.searchScenicSpotsByName(name);
        return Result.success(scenicSpots);
    }
    
    /**
     * 新增景点（管理员权限）
     */
    @PostMapping("/scenicspot/add")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<?> addScenicSpot(@RequestBody ScenicSpot scenicSpot) {
        ScenicSpotDTO savedScenicSpot = scenicSpotService.addScenicSpot(scenicSpot);
        return Result.success("添加成功", savedScenicSpot);
    }
    
    /**
     * 更新景点信息（管理员权限）
     */
    @PutMapping("/scenicspot/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<?> updateScenicSpot(@PathVariable Integer id, @RequestBody ScenicSpot scenicSpot) {
        ScenicSpotDTO updatedScenicSpot = scenicSpotService.updateScenicSpot(id, scenicSpot);
        return Result.success("更新成功", updatedScenicSpot);
    }
    
    /**
     * 删除景点（管理员权限）
     */
    @DeleteMapping("/scenicspot/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<?> deleteScenicSpot(@PathVariable Integer id) {
        scenicSpotService.deleteScenicSpot(id);
        return Result.success("删除成功");
    }
} 